﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UMLSynthesizer.Tasks;

namespace TaskLauncher.ViewModel
{
    class LauncherWindowViewModel : WorkspaceViewModel
    {
        #region Fields and RelayCommands

        string _selectedTask;
        string[] _taskParameters;

        RelayCommand _launchTaskCommand;

        #endregion

        #region Constructor

        /// <summary>
        /// TaskLauncher constructor
        /// 
        /// Parameters description:
        ///     -silent: Don't show the task selector interface and start the selected task
        ///     -selectTask "TaskName": Select the task with "TaskName"
        ///     -taskParameters "Parameters": Take all next parameters as Task's parameters
        /// </summary>
        public LauncherWindowViewModel(string[] parameters = null)
        {
            //Read parameters
            bool isForcedSilent = false;
            string forcedSelectedTask = null;
            List<string> taskParameters = new List<string>();
            bool readTaskParameters = false;

            if (parameters != null)
                for (int i = 0; i < parameters.Length; i++)
                    if (!readTaskParameters)
                        switch (parameters[i].ToLower())
                        {
                            case "-silent":
                                isForcedSilent = true;
                                break;

                            case "-selecttask":
                                if (++i < parameters.Length)
                                    forcedSelectedTask = parameters[i];
                                else
                                    throw new ArgumentException("Expected task name");
                                break;

                            case "-taskparameters":
                                readTaskParameters = true;
                                break;

                            default:
                                throw new ArgumentException("Invalid parameter: " + parameters[i]);
                        }
                    else
                        taskParameters.Add(parameters[i]);

            //Initialize
            AvailableTasks = new string[]
            {
                ClassDiagramTask.DisplayName,
                SelectProjectTask.DisplayName
            };

            if (forcedSelectedTask == null)
                SelectedTask = AvailableTasks.ElementAtOrDefault(0);
            else
                SelectedTask = forcedSelectedTask;

            TaskParameters = taskParameters.ToArray();

            //If silent, try to start the task
            if (isForcedSilent && LaunchTaskCommand.CanExecute(null))
                LaunchTaskCommand.Execute(null);
        }

        #endregion

        #region Properties

        /// <summary>
        /// The list of available tasks
        /// </summary>
        public IEnumerable<string> AvailableTasks { get; private set; }
        /// <summary>
        /// The selected task
        /// </summary>
        public string SelectedTask
        {
            get { return _selectedTask; }
            set
            {
                if (value == _selectedTask)
                    return;
                _selectedTask = value;
                OnPropertyChanged("SelectedTask");
            }
        }
        /// <summary>
        /// Task's parameters
        /// </summary>
        public string[] TaskParameters
        {
            get { return _taskParameters; }
            set
            {
                if (value == _taskParameters)
                    return;
                _taskParameters = value;
                OnPropertyChanged("TaskParameters");
            }
        }

        #endregion

        #region Commands

        public ICommand LaunchTaskCommand
        {
            get
            {
                if (_launchTaskCommand == null)
                {
                    _launchTaskCommand = new RelayCommand(
                        param =>
                        {
                            switch (SelectedTask)
                            {
                                case ClassDiagramTask.DisplayName:
                                    ClassDiagramTask A = null;
                                    A = new ClassDiagramTask(() =>
                                    {
                                        var B = A.ElementsToPrint;
                                    });
                                    A.StartTask(TaskParameters);
                                    break;
                                case SelectProjectTask.DisplayName:
                                    new SelectProjectTask().StartTask(TaskParameters);
                                    break;
                            }
                        },
                        param => SelectedTask != null);
                }
                return _launchTaskCommand;
            }
        }

        #endregion
    }
}
